package parsers;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 
 * @author aprokofjev
 * 
 */

public class LogCatParser extends BaseLogFileParser {

    private static final String DATE_FORMAT = "MM-dd HH:mm:ss.SSS";

    private static final Pattern ENTRY_BEGIN_PATTERN = Pattern
            .compile("^\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}.*$"); // pattern for entry begin

    private static final String EXTRA_APPLICATION_FIELD_KEY = "Application";

    private static final List<String> EXTRA_FIELDS_KEYS = Arrays.asList(EXTRA_APPLICATION_FIELD_KEY);

    private Map<Integer, String> mAppMap = new HashMap<Integer, String>();

    /**
     * Returns the name of this parser.
     * 
     * @see com.lightysoft.logmx.mgr.LogFileParser#getParserName()
     */
    @Override
    public String getParserName() {
        return "LogCat parser";
    }

    /**
     * Returns the supported file type for this parser.
     * 
     * @see com.lightysoft.logmx.mgr.LogFileParser#getSupportedFileType()
     */
    public String getSupportedFileType() {
        return "LogMX logcat log files";
    }

    @Override
    protected boolean addLine(String line) throws Exception {
        if (ENTRY_BEGIN_PATTERN.matcher(line).matches()) {
            // Record previous found entry if exists, then create a new one
            prepareNewEntry();
            fillEntry(line);
            String message = getMessage(line).trim();

            if ("ActivityManager".equals(getEmitter(line)) && message.startsWith("Start proc")) {
                String application = message.substring(message.indexOf("Start proc") + 11, message.indexOf(" for"));
                String appPid = message.substring(message.indexOf("pid=") + 4, message.indexOf(" uid"));
                mAppMap.put(new Integer(appPid), application);
            }
            putExtraValue(EXTRA_APPLICATION_FIELD_KEY, mAppMap.get(new Integer(getPID(line))));            
            return true;
        }
        return false;
    }

    

    public String getMessage(String line) {
        return line.substring(line.indexOf(')') + 2);
    }

    public String getEmitter(String line) {
        return line.substring(line.indexOf('/') + 1, line.indexOf('('));
    }

    public String getPID(String line) {
        return line.substring(line.indexOf('(') + 1, line.indexOf(')')).trim();
    }

    public String getDateFromLine(String line) {
        return line.substring(0, line.indexOf('.') + 4);
    }

    public String getLogLevel(String line) {
        char c = line.charAt(line.indexOf('/') - 1);
        switch (c) {
        case 'E':
        case 'A':
            return "SEVERE";
        case 'W':
            return "WARN";
        case 'I':
            return "INFO";
        case 'D':
            return "DEBUG";
        case 'V':
            return "TRACE";
        default:
            return "SEVERE";
        }
    }

    @Override
    public List<String> getUserDefinedFields() {
        return EXTRA_FIELDS_KEYS;
    }

    @Override
    public String getDateFormat() {
        return DATE_FORMAT;
    }

}
